package com.example.shiwuyouproject.ui.fragment.my.activity
import android.content.Intent
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivityRealnameauthenticationBinding
import com.example.shiwuyouproject.net.bean.CoomStatus
import com.example.shiwuyouproject.net.http.QiNiuUpImageUtils
import com.example.shiwuyouproject.ui.bean.UpDataMessage
import com.example.shiwuyouproject.ui.fragment.my.activity.viewmodel.RealNameAuthenticationViewModel
import com.example.shiwuyouproject.ui.my.bean.PersonalInformationBean
import com.example.shiwuyouproject.utils.GlideUtils
import com.luck.picture.lib.config.PictureConfig
import com.yechaoa.yutilskt.ToastUtil
import com.yechaoa.yutilskt.YUtils
import org.greenrobot.eventbus.EventBus
import java.util.*
/**
 * 实名认证
 */
class RealNameAuthenticationActivity :
    BaseVmActivity<ActivityRealnameauthenticationBinding, RealNameAuthenticationViewModel>() {
    override fun viewModelClass(): Class<RealNameAuthenticationViewModel> =
        RealNameAuthenticationViewModel::class.java

    override fun getViewBinding(): ActivityRealnameauthenticationBinding {
        return ActivityRealnameauthenticationBinding.inflate(layoutInflater)
    }

    private var carPthZhen = ""
    private var carPthFan = ""
    private var cardStatus = false//false正面true反面
    override fun initView() {
        mBinding.clTitleBar.tvTitle.text = "实名认证"

    }

    override fun initData() {
        mViewModel.getRealNameAuthenticationData()

    }

    override fun setListener() {
        mBinding.clTitleBar.tvBack.setOnClickListener { finish() }
        mBinding.clUploadTheFrontOfIDCard.setOnClickListener { //上传身份证正面
            cardStatus = false
            QiNiuUpImageUtils.openCard(this)
        }
        mBinding.clUploadBackOfIDCard.setOnClickListener { //上传身份证反面
            cardStatus = true
            QiNiuUpImageUtils.openCard(this)
        }
        mBinding.btShiBie.setOnClickListener {
            if (carPthZhen.isBlank()) {
                ToastUtil.show("请上传身份证正面图片！")
                return@setOnClickListener
            }
            if (carPthFan.isBlank()) {
                ToastUtil.show("请上传身份证反面图片！")
                return@setOnClickListener
            }
            YUtils.showLoading(this, "上传中...")
            mViewModel.preservationCard(carPthZhen, carPthFan)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK&&requestCode==PictureConfig.REQUEST_CAMERA) {
                QiNiuUpImageUtils.postUpQiNiuFile(data, object :
                    QiNiuUpImageUtils.onBackImageStatus {
                    override fun onSuccess(key: String) {
                        if (!cardStatus) {//正面
                            carPthZhen = key
                            GlideUtils.fangImgPortrait(
                                this@RealNameAuthenticationActivity,
                                mBinding.ivUploadIDCard,
                                carPthZhen
                            )
                        } else {//反面
                            carPthFan = key
                            GlideUtils.fangImgPortrait(
                                this@RealNameAuthenticationActivity,
                                mBinding.ivUploadBackOfIDCard,
                                carPthFan
                            )
                        }
                        setBootStatus()
                    }
                    override fun onFail() {
                        ToastUtil.show("图片上传失败，请检查网络连接")
                    }
                })
                }
    }
    /**
     * 检测按钮颜色状态
     */
    private fun setBootStatus() {
        if (carPthZhen.isBlank()) {
            mBinding.btShiBie.isSelected = false
            return
        }
        if (carPthFan.isBlank()) {
            mBinding.btShiBie.isSelected = false
            return
        }
        mBinding.btShiBie.isSelected = true
    }

    override fun observe() {
        mViewModel.getGenRenStatus.observe(this, androidx.lifecycle.Observer<PersonalInformationBean>{
            carPthZhen = it.idfront
            carPthFan = it.idback
            GlideUtils.fangImgPortrait(this, mBinding.ivUploadIDCard,   carPthZhen)
            GlideUtils.fangImgPortrait(
                this,
                mBinding.ivUploadBackOfIDCard,
                 carPthFan
            )
            setBootStatus()
        })
        mViewModel.getPostStatus.observe(this, androidx.lifecycle.Observer<Boolean>{
            YUtils.hideLoading()
            if (it) {
                ToastUtil.show("身份图片，上传成功！")
                EventBus.getDefault().post(UpDataMessage(CoomStatus.UPMESSAGE_TYPE))
                finish()
            } else {
                ToastUtil.show("身份图片，上传失败！")
            }

        })

    }
}